using VisualWip.Core.Interfaces;

namespace VisualWip.Core.Columns
{
    public abstract class WorkDualQueryColumnBase : WorkColumnBase
    {
        private IQuery _firstQuery;

        private IQuery _secondQuery;

        protected WorkDualQueryColumnBase(IWorkItemProviderFactory workItemProviderFactory)
            : base(workItemProviderFactory)
        {
        }

        public IQuery FirstQuery
        {
            get { return _firstQuery; }
            set
            {
                if (value != _firstQuery)
                {
                    _firstQuery = value;
                    OnPropertyChanged(this, () => FirstQuery);
                }
            }
        }

        public IQuery SecondQuery
        {
            get { return _secondQuery; }
            set
            {
                if (value != _secondQuery)
                {
                    _secondQuery = value;
                    OnPropertyChanged(this, () => SecondQuery);
                }
            }
        }

        public override int WorkInProgressLimit
        {
            get { return GetWorkInProgressLimit(); }
        }


        public override bool IsOverWorkInProgressLimit
        {
            get { return WorkInProgressLimit < WorkInProgress; }
        }

        public override bool IsAtWorkInProgressLimit
        {
            get { return WorkInProgressLimit == WorkInProgress; }
        }

        public override bool IsInWorkInProgressLimit
        {
            get { return WorkInProgressLimit >= WorkInProgress; }
        }

        public override PersistenceTree Persist(string name)
        {
            PersistenceTree settings = base.Persist(name);
            settings.AddChild(() => FirstQuery);
            settings.AddChild(() => SecondQuery);
            return settings;
        }

        public override void Load(PersistenceTree settings)
        {
            base.Load(settings);
            FirstQuery = settings.GetChild(() => FirstQuery);
            SecondQuery = settings.GetChild(() => SecondQuery);
        }

        protected int SecondColumnWorkCount()
        {
            return WorkItemProvider.GetWorkCount(SecondQuery);
        }

        protected int FirstColumnWorkCount()
        {
            return WorkItemProvider.GetWorkCount(FirstQuery);
        }

        public abstract int GetWorkInProgressLimit();
    }
}